package com.bauer.java.datagen.es.client;

import com.alibaba.fastjson.JSON;
import com.bauer.java.datagen.mysql.po.Sales;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.util.Random;

/**
 * @Author ：rui.wei
 * @Date ：Created in 17:35 2019/7/29
 * @Description：
 */
public class EsClinet {

    public static void main(String[] args) throws Exception {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("99.48.58.219", 9200, "http")));

        String[] coutry = new String[]{"江苏"};
        String[] citys = new String[]{"南京", "苏州", "常州", "无锡"};

        for (String provice : coutry) {

            for (String city : citys) {

                for (int i = 2010; i <= 2018; i++) {
                    for (int j = 1; j <= 12; j++) {
                        BulkRequest bulkRequest = new BulkRequest("sales","doc");

                        for (int k = 1; k <= 30; k++) {

                            Sales sales = new Sales();
                            sales.setCountry(provice);
                            sales.setCity(city);
                            sales.setYear(i);
                            sales.setMoth(j);
                            sales.setDay(k);
                            sales.setAmount(new Random().nextInt(100000));
                            bulkRequest.add(new IndexRequest().id(provice + "_" + city + "_" + i + "_" + j + "_" + k)
                                    .source(JSON.toJSONString(sales), XContentType.JSON));
                        }
                        BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);

                        System.out.println(JSON.toJSONString(bulkResponse));
                    }
                }

            }
        }

        client.close();
    }
}
